﻿#include "stdafx.h"
#include "core.h"
#include "export_functions.h"
#include "functions.h"
#include "common_types.h"
#include "PlaySound.h"
  

CORE_API void CoreGetVersionInfo(int* pVersMajor, int* pVersionMinor, int* pBuildNumber)
{
	*pVersMajor = CORE_VERSION_MAJOR;
	*pVersionMinor = CORE_VERSION_MINOR;
	*pBuildNumber = CORE_VERSION_BUILDNUMBER;
 
}
 

CORE_API HRESULT CoreInit( PCoreInitData pcid , PGuiCallbackData pccd )
{
	// if(g_core)
	// {
	//	 HandleError(NULL, 0, "g_core is not NULL");
	//	 return E_FAIL;
	// }

	// g_core =  new Core();



	


	DoCreateCore();
	GetGore()->Initialize(pcid, pccd);


	// cрeck ini
	{
		Config* cfg = GetGore()->GetGonfig();
		cfg->get_value("test", "test", "value");
		cfg->Save();
	}


	// check sound
	if(0)
	{
		std::wstring file = GetGore()->GetSoundDirectory();
		file += L"test.wav";
		int temp = PlaySoundFile(file.c_str());


	}
	// end (check sound)

	// test Core::GetExeDirectory()
	if(0)
	{
		std::wstring temp = Core::GetExeDirectory();
		temp = L"";
	}

	 return 0;
} 

CORE_API void CoreFinalize( PGuiCallbackData pccd )
{
	GetGore()->Finalize();
}



FILE* g_logfile = NULL;

CORE_API void CoreLogMessage(const char* msg)
{
	if(!g_logfile)
	{
		// create log file
		WCHAR temp [MAX_PATH];
		ksfilefunc::getApplPathW(temp);
		std::wstring filename (temp);
		filename += L"log.txt";
		g_logfile = _wfopen(filename.c_str(), L"w+b");
		if(!g_logfile)
		{
			MessageError("Can`t create log file !");
			exit(-1);
		}

		fwrite(msg, strlen(msg), 1, g_logfile );
		
	}
 
}

void CoreHandleError(int flags, int id, const char* msg)
{
 
	char temp[2048];
	sprintf(temp, "CoreHandleError. flags(%i)  id(%i)  %s", flags, id, msg);
	CoreLogMessage(temp);

	if( flags & ERROR_FLAG_SHOWMESSAGEBOX )
	{
		MessageError(std::string(temp));
	}

	if( flags & ERROR_FLAG_CRITICAL )
	{
		exit( id );
	}

}
 
CORE_API void CoreSelectUser(PGuiCallbackData pccd)
{
	std::string usersline = GetGore()->GetUserInfoList()->MakeUsersLine();
	int res = pccd->func_CallFromDll_DialogSelectUser(usersline.c_str());
	if(res == -1) 
	{
		return;
	}

	std::string username = 
		GetGore()->GetUserInfoList()->GetUser( (size_t)res )->GetUserName();


	GetGore()->SelectUser(username);
}

CORE_API void CoreGuiEventNotify(TGuiEventNotifyInfo* pgeni)
{
	
	//int temp =
	//pgeni->formOwner;
	pgeni->formOwner = NULL; 
	assert(0);




}

int CoreGetConfigValue(char* dest, int destsize, const char* section, const char* key, const char* default)
{
	Config* cfg = GetGore()->GetGonfig();
	std::string res = cfg->get_value( std::string(section), std::string(key), std::string(default)  );

	strncpy(dest, res.c_str(), destsize );
	return 0;
}

int CoreSetConfigValue(const char* section, const char* key, const char* value)
{
	Config* cfg = GetGore()->GetGonfig();
	cfg->set_value( std::string(section), std::string(key), std::string(value) );
	return 0;
}



CORE_API int CoreDictGetNumWords(PGuiCallbackData pccd)
{
	int res = GetGore()->GetDictionary()->GetNumWords();
 	return res;
}

 
CORE_API bool CoreDictGetWorldInfoWord (PGuiCallbackData pccd, char* dest_rus, const char* engWord)
{
	bool res = GetGore()->GetDictionary()->GetWorldInfoWord(dest_rus, engWord);
	return res;
}
 
CORE_API bool CoreDictGetWorldInfoIndex(PGuiCallbackData pccd, char* dest_eng, char* dest_rus, const int engWordIndex)
{
	bool res = GetGore()->GetDictionary()->GetWorldInfoIndex(dest_eng, dest_rus, engWordIndex);
	return res;
}

CORE_API bool CoreDictAddNewWord(PGuiCallbackData pccd, const char* eng, const char* rus_line)
{
	bool res = GetGore()->GetDictionary()->AddNewWord(eng, rus_line);
	return res;
}

CORE_API bool CoreDictModifyWordIndex(PGuiCallbackData pccd, const char* rus, const int engWordindex)
{
	bool res = GetGore()->GetDictionary()->ModifyWordIndex(rus,  engWordindex);
	return res;
}

bool CoreDictDeleteWordIndex(PGuiCallbackData pccd, const int engWordIndex)
{
	bool res = GetGore()->GetDictionary()->DeleteWordIndex( engWordIndex);
	return res;
}

int CoreDictSpeakWord(const char* word)
{
	if(!word)
	{
		return -1;
	}

	const size_t LEN = strlen(word);
	if(LEN > 32)
	{
		return -1;
	}

	std::wstring fullpath = GetGore()->GetSoundDirectory();

	if(fullpath.length() == 0)
	{
		MessageError("Директория озвучивания слов не задана. \n\
		Задайте директорию в настройках.");
	}

	// make full path
	{
		std::wstring temp;

		// make suffix
		{
			char buf[2];
			buf[0] = word[0];
			buf[1] = '\0';
			temp = ksstr::convertAstringToWstringWinapi( std::string( buf ) );
			fullpath += temp;
			fullpath += L"\\";
		}
		// end (make suffix)
		
		temp = ksstr::convertAstringToWstringWinapi( std::string(word) );
		fullpath += temp;

	}

	// check exists
	if( !ksfilefunc::fileExistsW(fullpath.c_str()) )
	{
		return -1;
	}

	int res = PlaySoundFile(fullpath.c_str());
	if( res )
	{
		return -1;
	}


	//assert(false && " int CoreDictSpeakWord(const char* word) "); return -1;



	return 0;
}